<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Round extends CI_Controller
{
    protected $menu = array(
	'menu_1' => '',
	'menu_2' => '',
	'menu_3' => 'active',
	'menu_4' => '',
    );
    
    function __construct()
    {
	parent::__construct();
	
	if( ! $this->_checkAuth()){
	    redirect('admin/login');
	    exit;
	}
    }
    
    function index()
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->initmodel->set_table_name('rounds');
	$this->initmodel->set_where_not_equal(array('status'=>'0'));
	$this->initmodel->set_group_by('date');
	$this->initmodel->set_order_by(array('date'=>'desc'));
	$temp = $this->initmodel->load_records();
	
	if($temp==''){
	    $temp = array();
	    $var = array();
	}
	$i = 0;
	foreach($temp as $val){
	    $var['date'][$i] = $val;
	    $this->initmodel->set_where(array('date'=>$val['date']));
	    $this->initmodel->set_where_not_equal(array('status'=>'0'));
	    $this->initmodel->set_order_by(array('time_period'=>'asc'));
	    $prizes_temp = $this->initmodel->load_records();
	    
	    $j = 0;
	    foreach($prizes_temp as $val2){
		$var['date'][$i]['prizes'][$j]['id'] = $val2['id'];
		$var['date'][$i]['prizes'][$j]['prize_id'] = $val2['prize_id'];
		$var['date'][$i]['prizes'][$j]['question_id'] = $val2['question_id'];
		
		$this->load->model('prizes_model');
		$this->prizes_model->set_where(array('id'=>$val2['prize_id']));
		$temp2 = $this->prizes_model->load_records();
		$var['date'][$i]['prizes'][$j]['prize_detail_img'] = $temp2['0']['detail_img'];
		
		$this->load->model('question_model');
		$this->question_model->set_where(array('id'=>$val2['question_id']));
		$temp2 = $this->question_model->load_records();
		$var['date'][$i]['prizes'][$j]['question_detail_img'] = $temp2['0']['question_img'];
		
		switch($val2['time_period']){
		    case 1 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '00:00 - 00:59';
			break;
		    case 2 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '01:00 - 01:59';
			break;
		    case 3 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '02:00 - 02:59';
			break;
		    case 4 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '03:00 - 03:59';
			break;
		    case 5 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '04:00 - 04:59';
			break;
		    case 6 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '05:00 - 05:59';
			break;
		    case 7 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '06:00 - 06:59';
			break;
		    case 8 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '07:00 - 07:59';
			break;
		    case 9 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '08:00 - 08:59';
			break;
		    case 10 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '09:00 - 09:59';
			break;
		    case 11 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '10:00 - 10:59';
			break;
		    case 12 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '11:00 - 11:59';
			break;
		    case 13 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '12:00 - 12:59';
			break;
		    case 14 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '13:00 - 13:59';
			break;
		    case 15 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '14:00 - 14:59';
			break;
		    case 16 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '15:00 - 15:59';
			break;
		    case 17 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '16:00 - 16:59';
			break;
		    case 18 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '17:00 - 17:59';
			break;
		    case 19 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '18:00 - 18:59';
			break;
		    case 20 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '19:00 - 19:59';
			break;
		    case 21 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '20:00 - 20:59';
			break;
		    case 22 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '21:00 - 21:59';
			break;
		    case 23 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '22:00 - 22:59';
			break;
		    case 24 :
			$var['date'][$i]['prizes'][$j]['time_period'] = '23:00 - 23:59';
			break;
		}
		
		$j++;
	    }
	    
	    $i++;
	}
	
	
	$this->mytpl->set_template('admin-bootstrap/rounds.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($var);
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function add_form()
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->initmodel->set_table_name('question');
	$this->initmodel->set_where(array('status'=>'1'));
	$var['questions'] = $this->initmodel->load_records();
	$var['questions_list'] = '';
	foreach($var['questions'] as $val){
	    $var['questions_list'] .= "{$val['id']},";
	}
	
	$this->initmodel->set_table_name('prizes');
	$this->initmodel->set_where(array('status'=>'1'));
	$var['prizes'] = $this->initmodel->load_records();
	$var['prizes_list'] = '';
	foreach($var['prizes'] as $val){
	    $var['prizes_list'] .= "{$val['id']},";
	}
	
	$var['error_show'] = $this->session->flashdata('round_add_error_show');
	$var['error_msg'] = $this->session->flashdata('round_add_error_msg');
	$this->mytpl->set_template('admin-bootstrap/round-add-form.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($var);
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function add()
    {
	if ($this->input->post('post_check')) {
	    $check['status'] = TRUE;
	    $check['message'] = '';
	    $round_date = strtotime($this->input->post('round_date').' 00:00:00');
	    
	    $this->initmodel->set_table_name('rounds');
	    $this->initmodel->set_where(array('date'=>$round_date));
	    $this->initmodel->set_where_not_equal(array('status'=>'0'));
	    $dta = $this->initmodel->load_records();
	    if(is_array($dta)){// เช็คว่า  วันที่ระบุมีอยู่ในระบบแล้ว หรือเปล่า
		$check['status'] = FALSE;
		$check['message'] .= '* วันที่ระบุมีอยู่ในระบบแล้ว กรุณาเปลี่ยนหรือกลับไปแก้ไข<br />';
	    }
	    
	    $period_times = $this->input->post('period_time');
	    
	    if(in_array(0 , $period_times)){
		$check['status'] = FALSE;
		$check['message'] .= '* กรุณาระบุเวลาให้ครบ<br />';
	    }else{
		$period_times_count = array_count_values($period_times);
		$rand_time_all = FALSE;
		foreach($period_times_count as $key=>$val){ // เช็คว่า มีช่วงเวลาที่ตรงกัน หรือเปล่า
		    if($key != 0 AND $val > 1){
			$check['status'] = FALSE;
			$check['message'] .= '* มีช่วงเวลาที่ตรงกัน<br />';
		    }
		}
	    }
	    
	    $questions = $this->input->post('question_id');
	    $i = 2;
	    for($i;$i<=7;$i++){ // เช็คว่า มีคำถามที่เหมือนกัน อยู่ในช่วงเวลาที่ต่อเนื่องกัน หรือเปล่า
		if($questions[$i] != 0 AND ($questions[$i] == $questions[($i-1)] OR $questions[$i] == $questions[($i+1)])){
		    $check['status'] = FALSE;
		    $check['message'] .= '* มีคำถามที่เหมือนกัน อยู่ในช่วงเวลาที่ต่อเนื่องกัน<br />';
		    break;
		}
	    }
	    
	    if($check['status']){
		$prizes = $this->input->post('prize_id');
		$question_id_list = explode(',',substr($this->input->post('question_id_list'),0,-1));
		$prize_id_list = explode(',',substr($this->input->post('prize_id_list'),0,-1));
		
		$this->initmodel->set_table_name('rounds');
		
		$period_times_temp = $period_times;
		$questions_temp = $questions;
		$prizes_temp = $prizes;
		asort($period_times_temp);
		$i = 1;
		foreach ($period_times_temp as $key => $val) { // เรียบลำดับของรางวัล และคำถามใหม่ตามเวลา
		    $period_times[$i] = $period_times_temp[$key];
		    $questions[$i] = $questions_temp[$key];
		    $prizes[$i] = $prizes_temp[$key];
		    $i++;
		}
		//var_dump($period_times);
		//var_dump($questions);
		//var_dump($prizes);
		//exit;
		
		$i = 1;
		$input['date'] = $round_date;
		$questions_check = array();
		for($i;$i<=8;$i++){
		    $input['time_period'] = $period_times[$i];
		    
		    $input['prize_id'] = $prizes[$i];
		    if($input['prize_id'] == 0){ // ถ้าไม่ระบุของรางวัล จะแรนดอม โดยไม่คำนึงว่าจะซ้ำกับช่วงก่อนหน้า หรือช่วงถัดไปหรือเปล่า
			shuffle($prize_id_list);
			$input['prize_id'] = $prize_id_list[0];
		    }
		    
		    $input['question_id'] = $questions[$i];
		    if($input['question_id'] == 0){ //ถ้าไม่ระบุคำถาม จะแรนดอม โดยคำนึงว่าจะซ้ำกับช่วงก่อนหน้า หรือช่วงถัดไปหรือเปล่า
			$rand_keys = array_rand($question_id_list);
			
			if($i == 1){ // ถ้าเป็นรอบแรก และรอบที่สองไม่เท่ากับศูนย์ ให้แรนไม่ให้ตรงกับคำถามที่สอง
			    $input['question_id'] = $question_id_list[$rand_keys];
			    if($questions['2'] != 0){
				//a:
				//if($question_id_list[$rand_keys] == $questions['2']){
				//    $rand_keys = array_rand($question_id_list);
				//    goto a;
				//}
				
				shuffle($question_id_list);
				$count = count($question_id_list);
				$x = 0;
				for($x;$x<$count;$x++){
				    $rand_keys = $x;
				    if($question_id_list[$rand_keys] != $questions['2']){
					break;
				    }
				}
				
				$input['question_id'] = $question_id_list[$rand_keys];
			    }
			    
			}elseif($i == 8){ // ถ้า รอบสุดท้ายเท่ากับศูยน์ ให้แรนโดยเช็คกับรอบที่ 7
			    //if($questions['8'] == 0){
				//b:
				//if($question_id_list[$rand_keys] == $questions_check['7']){
				//    $rand_keys = array_rand($question_id_list);
				//    goto b;
				//}
				
				shuffle($question_id_list);
				$count = count($question_id_list);
				$x = 0;
				for($x;$x<$count;$x++){
				    $rand_keys = $x;
				    if($question_id_list[$rand_keys] != $questions_check['7']){
					break;
				    }
				}
				
				$input['question_id'] = $question_id_list[$rand_keys];
			    //}
			}else{ // เป็นรอบที่อยู่ในช่วง 2 - 7 ถ้าเป็นศูนย์ ให้ไปเช็คกับรอบที่แล้ว กับรอบถัดไป
			//    c:
			//    if(($question_id_list[$rand_keys] == $questions_check[($i-1)]) OR (($questions[($i+1)] !=0 AND $question_id_list[$rand_keys] == $questions[($i+1)]))){
			//	$rand_keys = array_rand($question_id_list);
			//	goto c;
			//    }
			    
			    shuffle($question_id_list);
			    $count = count($question_id_list);
			    $x = 0;
			    for($x;$x<$count;$x++){
				$rand_keys = $x;
				if(($question_id_list[$rand_keys] != $questions_check[($i-1)]) && (($questions[($i+1)] == 0 OR $question_id_list[$rand_keys] != $questions[($i+1)]))){
				    break;
				}
			    }
			    
			    $input['question_id'] = $question_id_list[$rand_keys];
			    
			}
		    }
		    
		    $questions_check[$i] =  $input['question_id'];
		    
		    //var_dump($input);
		    //echo '<hr />';
		    $prizes_id = $this->initmodel->insert_record($input);
		}
	    }
	    
	    //var_dump($check);
	    echo json_encode($check);
	    exit;
	}
	
	redirect('admin/round');
    }
    
    function delete($date)
    {
	$data['status'] = '0';
	$this->initmodel->set_table_name('rounds');
	$this->initmodel->set_where(array('date'=>$date));
	$this->initmodel->update_record($data);
	redirect('admin/round');
    }
    
    function user_date($date)
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->load->model('round_model','round_model');
	$this->round_model->set_where(array('date'=>$date));
	$rounds = $this->round_model->load_records();
	
	$var['user_answer'] = array();
	foreach($rounds as $key=>$val){
	    $var['date'] = date('d/m/Y', $val['date']);
	    
	    switch($val['time_period']){
		case 1 :
		    $var['user_answer'][$key]['time_period'] = '00:00 - 00:59';
		    break;
		case 2 :
		    $var['user_answer'][$key]['time_period'] = '01:00 - 01:59';
		    break;
		case 3 :
		    $var['user_answer'][$key]['time_period'] = '02:00 - 02:59';
		    break;
		case 4 :
		    $var['user_answer'][$key]['time_period'] = '03:00 - 03:59';
		    break;
		case 5 :
		    $var['user_answer'][$key]['time_period'] = '04:00 - 04:59';
		    break;
		case 6 :
		    $var['user_answer'][$key]['time_period'] = '05:00 - 05:59';
		    break;
		case 7 :
		    $var['user_answer'][$key]['time_period'] = '06:00 - 06:59';
		    break;
		case 8 :
		    $var['user_answer'][$key]['time_period'] = '07:00 - 07:59';
		    break;
		case 9 :
		    $var['user_answer'][$key]['time_period'] = '08:00 - 08:59';
		    break;
		case 10 :
		    $var['user_answer'][$key]['time_period'] = '09:00 - 09:59';
		    break;
		case 11 :
		    $var['user_answer'][$key]['time_period'] = '10:00 - 10:59';
		    break;
		case 12 :
		    $var['user_answer'][$key]['time_period'] = '11:00 - 11:59';
		    break;
		case 13 :
		    $var['user_answer'][$key]['time_period'] = '12:00 - 12:59';
		    break;
		case 14 :
		    $var['user_answer'][$key]['time_period'] = '13:00 - 13:59';
		    break;
		case 15 :
		    $var['user_answer'][$key]['time_period'] = '14:00 - 14:59';
		    break;
		case 16 :
		    $var['user_answer'][$key]['time_period'] = '15:00 - 15:59';
		    break;
		case 17 :
		    $var['user_answer'][$key]['time_period'] = '16:00 - 16:59';
		    break;
		case 18 :
		    $var['user_answer'][$key]['time_period'] = '17:00 - 17:59';
		    break;
		case 19 :
		    $var['user_answer'][$key]['time_period'] = '18:00 - 18:59';
		    break;
		case 20 :
		    $var['user_answer'][$key]['time_period'] = '19:00 - 19:59';
		    break;
		case 21 :
		    $var['user_answer'][$key]['time_period'] = '20:00 - 20:59';
		    break;
		case 22 :
		    $var['user_answer'][$key]['time_period'] = '21:00 - 21:59';
		    break;
		case 23 :
		    $var['user_answer'][$key]['time_period'] = '22:00 - 22:59';
		    break;
		case 24 :
		    $var['user_answer'][$key]['time_period'] = '23:00 - 23:59';
		    break;
	    }
	    
	    $this->load->model('user_answer_model','user_answer_model');
	    $this->user_answer_model->set_where(array('round_id'=>$val['id']));
	    $this->user_answer_model->set_order_by(array('is_win'=>'desc', 'date_add'=>'asc'));
	    $users_answer = $this->user_answer_model->load_records();
	    if(is_array($users_answer)){
		foreach($users_answer as $key2=>$val2){
		    $var['user_answer'][$key]['user_list'][$key2]['fb_id'] = $val2['fb_id'];
		    $var['user_answer'][$key]['user_list'][$key2]['name'] = $val2['name'];
		    $var['user_answer'][$key]['user_list'][$key2]['tel'] = '08'.substr($val2['tel'],1);
		    $var['user_answer'][$key]['user_list'][$key2]['card_no'] = $val2['card_no'];
		    $var['user_answer'][$key]['user_list'][$key2]['time_add'] = date('H:i:s', $val2['date_add']);
		    
		    switch($val2['is_win']){
			case 0 :
			    switch($val2['status']){
				case '' :
				    $var['user_answer'][$key]['user_list'][$key2]['status'] = 'ตอบคำถามไม่ถูกต้อง';
				    break;
				case '0' :
				    $var['user_answer'][$key]['user_list'][$key2]['status'] = 'ไม่อยู่ในช่วงเวลาที่มีการเล่น';
				    break;
				case '1' :
				    $var['user_answer'][$key]['user_list'][$key2]['status'] = 'ตอบคำถามของรอบนี้ไปแล้ว';
				    break;
				case '2' :
				    $var['user_answer'][$key]['user_list'][$key2]['status'] = 'ได้รับของรางวัลของวันไปแล้ว';
				    break;
				case '3' :
				    $var['user_answer'][$key]['user_list'][$key2]['status'] = 'ของรางวัลของรอบแจกครบไปแล้ว';
				    break;
				default :
				    $var['user_answer'][$key]['user_list'][$key2]['status'] = '';
			    }
			    break;
			case 1 :
			    $var['user_answer'][$key]['user_list'][$key2]['status'] = 'win';
			    break;
		    }
		}
	    }
	}
	
	//var_dump($var);exit;
	
	$this->mytpl->set_template('admin-bootstrap/round-user.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($var);
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    function user_time($round_id)
    {
	$this->mytpl->set_template('admin-bootstrap/menu.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($this->menu);
	$data['menu'] = $this->mytpl->html();
	
	$this->load->model('round_model','round_model');
	$this->round_model->set_where(array('id'=>$round_id));
	$round_info = $this->round_model->load_records();
	
	$var['date'] = date('d/m/Y', $round_info['0']['date']);
	
	switch($round_info['0']['time_period']){
	    case 1 :
		$var['time_period'] = '00:00 - 00:59';
		break;
	    case 2 :
		$var['time_period'] = '01:00 - 01:59';
		break;
	    case 3 :
		$var['time_period'] = '02:00 - 02:59';
		break;
	    case 4 :
		$var['time_period'] = '03:00 - 03:59';
		break;
	    case 5 :
		$var['time_period'] = '04:00 - 04:59';
		break;
	    case 6 :
		$var['time_period'] = '05:00 - 05:59';
		break;
	    case 7 :
		$var['time_period'] = '06:00 - 06:59';
		break;
	    case 8 :
		$var['time_period'] = '07:00 - 07:59';
		break;
	    case 9 :
		$var['time_period'] = '08:00 - 08:59';
		break;
	    case 10 :
		$var['time_period'] = '09:00 - 09:59';
		break;
	    case 11 :
		$var['time_period'] = '10:00 - 10:59';
		break;
	    case 12 :
		$var['time_period'] = '11:00 - 11:59';
		break;
	    case 13 :
		$var['time_period'] = '12:00 - 12:59';
		break;
	    case 14 :
		$var['time_period'] = '13:00 - 13:59';
		break;
	    case 15 :
		$var['time_period'] = '14:00 - 14:59';
		break;
	    case 16 :
		$var['time_period'] = '15:00 - 15:59';
		break;
	    case 17 :
		$var['time_period'] = '16:00 - 16:59';
		break;
	    case 18 :
		$var['time_period'] = '17:00 - 17:59';
		break;
	    case 19 :
		$var['time_period'] = '18:00 - 18:59';
		break;
	    case 20 :
		$var['time_period'] = '19:00 - 19:59';
		break;
	    case 21 :
		$var['time_period'] = '20:00 - 20:59';
		break;
	    case 22 :
		$var['time_period'] = '21:00 - 21:59';
		break;
	    case 23 :
		$var['time_period'] = '22:00 - 22:59';
		break;
	    case 24 :
		$var['time_period'] = '23:00 - 23:59';
		break;
	}
	    
	$this->load->model('user_answer_model','user_answer_model');
	$this->user_answer_model->set_where(array('round_id'=>$round_id));
	$this->user_answer_model->set_order_by(array('is_win'=>'desc', 'date_add'=>'asc'));
	$users_answer = $this->user_answer_model->load_records();
	if(is_array($users_answer)){
	    foreach($users_answer as $key2=>$val2){
		$var['user_list'][$key2]['fb_id'] = $val2['fb_id'];
		$var['user_list'][$key2]['name'] = $val2['name'];
		$var['user_list'][$key2]['tel'] = '08'.substr($val2['tel'],1);
		$var['user_list'][$key2]['card_no'] = $val2['card_no'];
		$var['user_list'][$key2]['time_add'] = date('H:i:s', $val2['date_add']);
		
		switch($val2['is_win']){
		    case 0 :
			switch($val2['status']){
			    case '' :
				$var['user_list'][$key2]['status'] = 'ตอบคำถามไม่ถูกต้อง';
				break;
			    case '0' :
				$var['user_list'][$key2]['status'] = 'ไม่อยู่ในช่วงเวลาที่มีการเล่น';
				break;
			    case '1' :
				$var['user_list'][$key2]['status'] = 'ตอบคำถามของรอบนี้ไปแล้ว';
				break;
			    case '2' :
				$var['user_list'][$key2]['status'] = 'ได้รับของรางวัลของวันไปแล้ว';
				break;
			    case '3' :
				$var['user_list'][$key2]['status'] = 'ของรางวัลของรอบแจกครบไปแล้ว';
				break;
			    default :
				$var['user_list'][$key2]['status'] = '';
			}
			break;
		    case 1 :
			$var['user_list'][$key2]['status'] = 'win';
			break;
		}
	    }
	}
	
	//var_dump($var);exit;
	
	$this->mytpl->set_template('admin-bootstrap/round-user-time.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($var);
	$data['content'] = $this->mytpl->html();
	
	$this->mytpl->set_template('admin-bootstrap/main.html');
	$this->mytpl->clear_cache();
	$this->mytpl->set_var($data);
	$this->mytpl->display();
    }
    
    /**
    * private function to use in this controller
    */
    function _checkAuth()
    {
	$admin = $this->session->userdata('admin');
	if (isset($admin['user']) && $admin['user'] == 'admin') {
	    return true;
	} else {
	    return false;
	}
    }
}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */